package org.bwgl.demo1.controller;

import io.swagger.annotations.Api;
import org.bwgl.demo1.mapper.UserMapper;
import org.bwgl.demo1.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@Api(tags = "用户信息")
public class UserController {


    @Autowired
    private UserMapper userMapper;
    /*= new UserMapper() {
    }; 由spring帮我们完成*/

    @GetMapping("user")

    public String getAllUser(){
//        List<User> users = userMapper.getAllUser();
        List<User> users = userMapper.selectList(null);
        System.out.println("users = " + users);
        return "得到所有用户信息";
    }

    @PostMapping("user")
    public String addUser(User user){
//        userMapper.addUser(user);
        userMapper.insert(user);
        return "增加用户";
    }

    @PutMapping("user")
    public String updateUser(User user){
//        userMapper.updateUser(user);
        userMapper.updateById(user);
        return "更新用户";
    }

    @DeleteMapping("user")
    public String deleteUser(){
        userMapper.delete(null);
        return "删除所有用户";
    }

    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable int id){
//        User user = userMapper.getUserById(id);
        User user = userMapper.selectById(id);
        System.out.println("user = " + user);
        return "得到一个用户的信息";
    }

    @DeleteMapping("/user/{id}")
    public String deleteById(@PathVariable int id){
//        userMapper.deleteUserById(id);
        userMapper.deleteById(id);
        return "删除一个用户";
    }

}
